Computer Graphics
|
GUI - Examples |
Program captures mouse
position and uses that as starting point for drawing a polygon.
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class PolyDraw
{
public
static GLCanvas canvas;
public
static void main(String[] args)
{
Frame
frame = new Frame("Follow the Mouse");
canvas = GLDrawableFactory.getFactory().createGLCanvas(new
GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static float xpos, ypos;
// mouse position
static
class Renderer extends Frame implements GLEventListener, KeyListener,
MouseListener
{
public
class cPt2D {
public
float x, y;
cPt2D(float xcoord, float ycoord){
x= xcoord;
y= ycoord;
}
cPt2D (
){
x = 0.0f;
y = 0.0f;
}
};
public
void translatePolygon (GL gl, cPt2D [] verts, int nVerts, float tx, float ty)
{
int k;
cPt2D
[] vertsNew = new cPt2D[4];
for (k
= 0; k < nVerts; k++)
vertsNew[k] = new cPt2D( );
for (k
= 0; k < nVerts; k++) {
vertsNew [k].x = verts [k].x + tx;
vertsNew [k].y = verts[k].y + ty;
}
gl.glBegin (GL.GL_POLYGON);
for
(k = 0; k < nVerts; k++)
gl.glVertex2f (vertsNew[k].x, vertsNew[k].y);
gl.glEnd ( );
}
int i=0; // color counter
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
float
[][] CLUT ={{1.0f,0.0f,0.0f}, {1.0f,0.5f,0.0f}, {1.0f,1.0f,0.0f}, {0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
//gl.glClear
(GL.GL_COLOR_BUFFER_BIT); // Set
display window to color.
//above line
commented out so framebuffer is not cleared
gl.glMatrixMode
(GL.GL_MODELVIEW);
gl.glLoadIdentity();
cPt2D
[] polygon = new cPt2D[4];
polygon[0] = new cPt2D(0.0f,0.0f);
polygon[1] = new cPt2D(50.0f,0.0f);
polygon[2] = new cPt2D(50.0f,50.0f);
polygon[3] = new cPt2D(0.0f,50.0f);
gl.glColor3fv (CLUT[i%8]);
translatePolygon(gl, polygon, 4, xpos, ypos);
i++; //
color counter
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
//
Set display window to color.
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f,
0.0f, 0.0f);
//set background to black
glu.gluOrtho2D (0, 499.0, 0.0, 499.0);
// define drawing area
gLDrawable.addKeyListener(this);
gLDrawable.addMouseListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {}
public void mouseEntered(MouseEvent event)
{}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event)
{}
public void mouseClicked(MouseEvent event)
{}
public void mousePressed(MouseEvent event) {
Component c = event.getComponent();
double width = c.getWidth();
double height = c.getHeight();
double x = event.getX();
double y = event.getY();
// scale to widow from viewport
xpos = (float)((x/width)*500);
ypos = (float)((y/height)*500);
ypos = 500.0f -ypos;
canvas.display();
}
}
}
Program captures mouse
position and uses that as starting point for drawing a line.
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class WhiteBoard1
{
public
static GLCanvas canvas;
public
static void main(String[] args)
{
Frame
frame = new Frame("White Board 1");
canvas =
GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static float xpos=0.0f,
ypos=0.0f, lastX=0.0f, lastY=0.0f;
static int drawcount=0;//checks to see if
mouse has entered before
static
class Renderer extends Frame implements GLEventListener, KeyListener,
MouseListener, MouseMotionListener
{
int i=0;
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
float
[][] CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
//gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode
(GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glColor3f(1.0f,1.0f,1.0f);
//drawcount used to make sure it
does not draw line from zero on loading
if (drawcount!=0){
gl.glBegin(GL.GL_LINE_STRIP);
gl.glVertex2f(lastX,lastY);
gl.glVertex2f(xpos,ypos);
gl.glEnd();
}
drawcount++;
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (0.0f, 500.0f, 0.0f, 500.0f); // define drawing area
gLDrawable.addKeyListener(this);
gLDrawable.addMouseListener(this);
gLDrawable.addMouseMotionListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public
void keyTyped(KeyEvent e) {
}
public void mouseEntered(MouseEvent event) {
Component c = event.getComponent();
double width = c.getWidth();
double height = c.getHeight();
double x = event.getX();
double y = event.getY();
// scale from image space to object space
lastX = (float)((x/width)*500);
lastY = (float)((y/height)*500);
lastY = 500.0f - lastY;
}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event)
{}
public void mouseClicked(MouseEvent event)
{}
public void mousePressed(MouseEvent event) {
Component c = event.getComponent();
double width = c.getWidth();
double height = c.getHeight();
double x = event.getX();
double y = event.getY();
// scale from image space to object space
lastX = (float)((x/width)*500);
lastY = (float)((y/height)*500);
lastY = 500.0f - lastY;
}
public void mouseMoved(MouseEvent event) {}
public void
mouseDragged(MouseEvent event) {
Component c = event.getComponent();
double width = c.getWidth();
double height = c.getHeight();
double x = event.getX();
double y = event.getY();
// scale from image space to object
space
xpos = (float)((x/width)*500);
ypos = (float)((y/height)*500);
ypos = 500.0f -ypos;
canvas.display();
lastX = xpos;
lastY = ypos;
}
}
}
Program captures mouse
position and uses that as starting point for drawing a line and drawing text.
import java.awt.*;
import java.awt.event.*;
import java.nio.IntBuffer;
import net.java.games.jogl.*;
import net.java.games.jogl.util.*;
public class WhiteBoard2
{
public
static GLCanvas canvas;
public
static void main(String[] args)
{
Frame
frame = new Frame("White Board 1");
canvas =
GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
frame.add(canvas);
frame.setSize(500, 500);
frame.addWindowListener(new WindowAdapter()
{
public
void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.show();
canvas.requestFocus();
}
static float xpos=0.0f,
ypos=0.0f, lastX=0.0f, lastY=0.0f;
static int drawcount=0;
static String s = "";
static boolean line=true;
static
class Renderer extends Frame implements GLEventListener, KeyListener,
MouseListener, MouseMotionListener
{
int i=0;
public
void display(GLDrawable gLDrawable)
{
final GL
gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
final
GLUT glut = new GLUT();
float
[][] CLUT ={{1.0f,0.0f,0.0f},{1.0f,0.5f,0.0f},{1.0f,1.0f,0.0f},
{0.0f,1.0f,0.0f},{0.0f,0.0f,1.0f},{1.0f,0.0f,1.0f},
{0.5f,0.0f,0.5f}, {1.0f,0.0f,0.5f}};
//gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glColor3f(1.0f,1.0f,1.0f);
if (line)
{
//drawcount used to make sure it
does not draw line from zero on loading
if
(drawcount!=0){
gl.glBegin(GL.GL_LINE_STRIP);
gl.glVertex2f(lastX,lastY);
gl.glVertex2f(xpos,ypos);
gl.glEnd();
}
drawcount++;
}
else
{
gl.glRasterPos2f(lastX,lastY);
glut.glutBitmapString(gl, 5, s);
lastX += glut.glutBitmapLength(5, s);
}
}
public
void init(GLDrawable gLDrawable)
{
final
GL gl = gLDrawable.getGL();
final
GLU glu = gLDrawable.getGLU();
gl.glClear (GL.GL_COLOR_BUFFER_BIT);
// Set display window to color.
gl.glMatrixMode (GL.GL_PROJECTION);
gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); //set background to white
glu.gluOrtho2D (0.0f, 500.0f, 0.0f, 500.0f); // define drawing area
gLDrawable.addKeyListener(this);
gLDrawable.addMouseListener(this);
gLDrawable.addMouseMotionListener(this);
}
public
void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean
deviceChanged)
{
}
public void reshape(GLDrawable gLDrawable,
int x, int y, int width, int height)
{
}
public
void keyPressed(KeyEvent e)
{
if
(e.getKeyCode() == KeyEvent.VK_ESCAPE)
System.exit(0);
}
public
void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e)
{
s =
String.valueOf(e.getKeyChar());
line
= false;
canvas.display();
}
public void mouseEntered(MouseEvent event) {
Component c = event.getComponent();
double
width = c.getWidth();
double
height = c.getHeight();
double x
= event.getX();
double y
= event.getY();
// scale
from image space to object space
lastX =
(float)((x/width)*500);
lastY =
(float)((y/height)*500);
lastY =
500.0f - lastY;
}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event)
{}
public void mouseClicked(MouseEvent event)
{}
public void mousePressed(MouseEvent event) {
Component c = event.getComponent();
double
width = c.getWidth();
double
height = c.getHeight();
double x
= event.getX();
double y = event.getY();
// scale
from image space to object space
lastX =
(float)((x/width)*500);
lastY =
(float)((y/height)*500);
lastY =
500.0f - lastY;
}
public void mouseMoved(MouseEvent event) {}
public
void mouseDragged(MouseEvent event) {
Component c = event.getComponent();
double
width = c.getWidth();
double
height = c.getHeight();
double x
= event.getX();
double y
= event.getY();
// scale
from image space to object space
xpos =
(float)((x/width)*500);
ypos =
(float)((y/height)*500);
ypos =
500.0f -ypos;
line = true;
canvas.display();
lastX =
xpos;
lastY =
ypos;
}
}
}